package com.git.hui.yinshua.biz.order.service.convert;

import com.alibaba.fastjson.JSONArray;
import com.git.hui.yinshua.api.model.constants.GlobalConstants;
import com.git.hui.yinshua.api.model.order.OrderTypeEnum;
import com.git.hui.yinshua.biz.order.dao.entity.OrderEntity;
import com.git.hui.yinshua.biz.order.dao.entity.OrderItemEntity;
import com.git.hui.yinshua.biz.order.dao.entity.PtOrderEntity;
import com.git.hui.yinshua.biz.order.dao.entity.PtOrderItemEntity;
import com.git.hui.yinshua.biz.rest.models.goods.rsp.GoodsSpecItem;
import com.git.hui.yinshua.biz.rest.models.order.rsp.OrderDto;
import com.git.hui.yinshua.biz.rest.models.order.rsp.OrderItemDto;
import com.git.hui.yinshua.biz.rest.models.order.rsp.OrderReceiveDto;
import com.git.hui.yinshua.biz.rest.models.order.rsp.PtOrderDto;
import com.git.hui.yinshua.biz.rest.models.order.rsp.PtOrderMemberDto;
import com.git.hui.yinshua.biz.rest.models.user.res.UserInfoDto;
import com.git.hui.yinshua.biz.util.ImgUtil;
import com.git.hui.yinshua.biz.util.PriceUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YiHui
 * @date 2024/6/20
 */
public class OrderConvert {

    public static OrderItemDto toOrderItem(OrderItemEntity entity) {
        OrderItemDto dto = new OrderItemDto();
        dto.setItemId(entity.getId());
        dto.setGoodsId(entity.getGoodsId());
        dto.setOrderPrice(PriceUtil.toYuanPrice(entity.getOrderPrice()));
        dto.setGoodsPrice(PriceUtil.toYuanPrice(entity.getGoodsPrice()));
        dto.setOrderQuantity(entity.getOrderQuantity());
        dto.setGoodsName(entity.getGoodsName());
        dto.setGoodsIntro(entity.getGoodsIntro());
        dto.setGoodsThumb(ImgUtil.toImgList(entity.getGoodsThumb()));
        dto.setOriginalImgs(ImgUtil.toImgList(entity.getOriginalImgs()));
        dto.setUploadImgs(ImgUtil.toImgList(entity.getUploadImgs()));
        dto.setSpecId(entity.getSpecId());
        dto.setSpec(entity.getSpec());
        dto.setBuyCnt(entity.getBuyCnt());
        dto.setRemark(entity.getRemark());
        if (StringUtils.isNotBlank(entity.getSpec())) {
            dto.setSpecItems(JSONArray.parseArray(entity.getSpec(), GoodsSpecItem.class));
        }
        return dto;
    }

    public static List<OrderItemDto> batchToOrderItems(List<OrderItemEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }

        return list.stream().map(OrderConvert::toOrderItem).collect(Collectors.toList());
    }

    public static OrderDto toOrder(OrderEntity entity) {
        OrderDto order = new OrderDto();
        order.setOrderId(entity.getId());
        order.setOrderAmount(PriceUtil.toYuanPrice(entity.getOrderAmount()));
        order.setOriginalAmount(PriceUtil.toYuanPrice(entity.getOriginalAmount()));
        order.setPayAmount(PriceUtil.toYuanPrice(entity.getPayAmount()));
        order.setPayStatus(entity.getPayStatus());
        order.setShippingAmount(PriceUtil.toYuanPrice(entity.getShippingAmount()));
        order.setOrderStatus(entity.getOrderStatus());
        order.setRemark(entity.getRemark());
        order.setOrderTime(entity.getOrderTime() == null ? null : entity.getOrderTime().getTime());
        order.setLatestPayTime(entity.getOrderTime() == null ? null : entity.getOrderTime().getTime() + GlobalConstants.OrderApi.PAY_INTERVAL_TIME);
        order.setPaySuccessTime(entity.getPaySuccessTime() == null ? null : entity.getPaySuccessTime().getTime());
        order.setDeliveryTime(entity.getDeliveryTime() == null ? null : entity.getDeliveryTime().getTime());
        order.setDeleted(entity.getDeleted());
        order.setCreateAt(entity.getCreateAt() == null ? null : entity.getCreateAt().getTime());
        order.setUpdateAt(entity.getUpdateAt() == null ? null : entity.getUpdateAt().getTime());
        order.setDownloadCnt(entity.getDownloadCnt() == null ? 0 : entity.getDownloadCnt());
        order.setType(entity.getType() == null ? OrderTypeEnum.NORMAL.getType() : entity.getType());

        OrderReceiveDto receive = new OrderReceiveDto();
        receive.setReceivingAddress(entity.getReceivingAddress());
        receive.setReceivingArea(entity.getReceivingArea());
        receive.setReceivingPerson(entity.getReceivingPerson());
        receive.setReceivingPhone(entity.getReceivingPhone());
        receive.setReceivingTime(entity.getReceivingTime() == null ? null : entity.getReceivingTime().getTime());
        receive.setTrackingCode(entity.getTrackingCode());
        receive.setTrackingCompanyCode(entity.getTrackingCompanyCode());
        order.setReceiveInfo(receive);

        order.setOrderItemList(batchToOrderItems(entity.getOrderItemList()));

        UserInfoDto user = new UserInfoDto();
        user.setUserId(entity.getUid());
        order.setUser(user);
        return order;
    }

    public static List<OrderDto> batchToOrders(List<OrderEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(OrderConvert::toOrder).collect(Collectors.toList());
    }

    public static PtOrderMemberDto toPtMember(PtOrderItemEntity item) {
        PtOrderMemberDto memberDto = new PtOrderMemberDto();
        memberDto.setUid(item.getUid());
        memberDto.setNickName(item.getNickname());
        memberDto.setAvatar(item.getAvatar());
        memberDto.setStatus(item.getPtStatus());
        memberDto.setOrderId(item.getRelatedOrderId());
        // 仅 ptStatus == 0 时，下面的时间才有效
        memberDto.setExpireTime(item.getParticipationTime().getTime() + GlobalConstants.OrderApi.PAY_INTERVAL_TIME);
        return memberDto;
    }

    public static PtOrderDto toPtOrder(PtOrderEntity entity) {
        PtOrderDto dto = new PtOrderDto();
        dto.setTitle(entity.getTitle());
        dto.setPtOrderId(entity.getId());
        dto.setPtCode(entity.getCode());
        dto.setPtNum(entity.getPtNum());
        dto.setPtPrice(PriceUtil.toYuanPrice(entity.getPtPrice()));
        dto.setStatus(entity.getPtStatus());
        dto.setStartTime(entity.getStartTime().getTime());
        dto.setEndTime(entity.getEndTime().getTime());
        dto.setMembers(entity.getItemList().stream().map(OrderConvert::toPtMember).collect(Collectors.toList()));
        return dto;
    }

    public static List<PtOrderDto> batchToPtOrders(List<PtOrderEntity> list) {
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list.stream().map(OrderConvert::toPtOrder).collect(Collectors.toList());
    }
}
